Oitsm.DeviceListPanel = Ext.extend(Ext.Panel, {
	layout : 'border',
	
	TYPE_GROUP : 0,
	TYPE_DEVICE : 1,
	TYPE_MONITOR_GOURP : 2,
	TYPE_MONITOR_NODE : 3,
	
	treeContextMenu : null,
	
	initComponent : function(){
		Oitsm.DeviceListPanel.superclass.initComponent.call(this);
		
		this.createDeviceTree();
		this.createStatusPanel();
	},
	
	showGroupAddForm : function(){
		var form = Ext.create('Ext.form.Panel', {
	        defaultType: 'textfield',
	        border: false,
	        frame : true,
	        items: [{
	        	xtype : 'hidden',
	        	name : 'parent',
	        	value : this.treeContextMenu._parent
	        },{
	        	name : 'name',
	            fieldLabel: '名称',
	            fieldWidth: 60,
	            labelAlign : 'right',
	            maxLength : 30,
	            msgTarget: 'side',
	            allowBlank: false
	        }]
	    });
	    
		var win = Ext.create('Ext.window.Window', {
	        title: '添加分组',
	        width: 300,
	        height: 100,
	        layout: 'fit',
	        plain:true,
	        items: form,
	        bbar : ['->',{
	        	iconCls : 'save',
	            text: '保存',
	            handler : function(){
	            	if(!form.getForm().isValid()){
						Ext.Msg.alert("提示","带红色线的项必须正确填写!");	            		
	            	}else{
	            		form.getForm().submit({
	            			url: webContext + '/node_addGroupNode.action',
	            			success: function(form, action) {
						       Ext.Msg.alert('提示', "添加成功");
						       win.close();
						    },
						    failure : function(){
						    	Ext.Msg.alert('提示', "添加失败!");
						    }
	            		});
	            	}
	            }
	        },'-',{
	            text: '取消',
	            iconCls : 'close',
	           	handler : function(){
	           		win.close();
	           	}
	        }]
	    });
	
	    win.show();
	},
	
	// 创建设备树
	createDeviceTree : function(){
		var addMenu = Ext.create('Ext.Action', {
	        iconCls: 'add',
	        text: '添加分组',
	        handler: function(widget, event) {
	        	this.showGroupAddForm();
	        },
	        scope : this
	    });
	    
	    function validMenu(type){
	    	addMenu.setDisabled(type == this.TYPE_MONITOR_GOURP || type == this.TYPE_MONITOR_NODE );
	    }
		
		var contextMenu = Ext.create('Ext.menu.Menu',{
			items : [addMenu]
		});
		this.treeContextMenu = contextMenu;
		
		var store = Ext.create('Ext.data.TreeStore', {
	        proxy: {
	            type: 'ajax',
	            url: webContext + '/moCache_getNodes.action'
	        }
	    });
		
		var tree = Ext.create('Ext.tree.Panel',{
			store: store,
        	rootVisible: false,
			title : '设备列表',
			collapsible : true,
			collapseMode : 'header',
			region : 'west',
			border : false,
			width : 200,
			split : true
		});
		this.add(tree);
		tree.addListener('itemcontextmenu',function(widget,r,item,index,e){
			e.stopEvent();
			validMenu.call(this,r.raw.type);
			contextMenu._parent = r.raw.id;
            contextMenu.showAt(e.getXY());
            return false;
		},this);
	},
	
	// 创建状态面板
	createStatusPanel : function(){
		var statusPanel = Ext.create('Ext.panel.Panel',{
			border : false,
			region : 'center'
		});	
		this.add(statusPanel);
	}
});